#include "control.h"

struct Tick_typedef tick;
struct Data_typedef data;

void Power_init(void)
{
    LCD_Clear(Black);
    LCD_SetBackColor(Black);
    LCD_SetTextColor(White);
	
	LED_Disp(0x00);
	
	HAL_TIM_Base_Start_IT(&htim6);
	
	EEPREOM_WriteByte(0, 0x03);
}

void LCD_Proc(void)
{
	if (uwTick - tick.lcd < 100)
		return;
	tick.lcd = uwTick;
	
	LCDPrintf(Line0, "Hello ID:%03d  ", 1);
	LCDPrintf(Line1, "lanqiaobei T:%d   ", data.tick10ms);
	
	LCDPrintf(Line5, "eeprom:%d   ", data.eep);
	
	LCDPrintf(Line8, "Data:%d    ", data.long_data);
	
}

void LED_Proc(void)
{
	static uint8_t led_flag;
	if (uwTick - tick.led < 300)
		return;
	tick.led = uwTick;
	
	data.LED_State = 0x3 << (led_flag%8);
	
	led_flag += 2;
	
	LED_Disp(data.LED_State);
}

void Key_Proc(void)
{
	if (uwTick - tick.key < 10)
		return;
	tick.key = uwTick;
	
	if (key[1].short_flag == 1)
	{
		key[1].short_flag = 0;
		LCDPrintf(Line9, "K1 short press   ");
		
		data.eep = EEPROM_ReadByte(0x00);
	}
	
	if (key[2].short_flag == 1)
	{
		key[2].short_flag = 0;
		LCDPrintf(Line9, "K2 short press   ");
	}
	
	if (key[3].short_flag == 1)
	{
		key[3].short_flag = 0;
		LCDPrintf(Line9, "K3 short press   ");
	}
	
	if (key[4].short_flag == 1)
	{
		key[4].short_flag = 0;
		LCDPrintf(Line9, "K4 short press   ");
	}
	
	if (key[4].long_flag == 1)
	{
		LCDPrintf(Line9, "K4 long  press   ");
		data.long_data ++;
	}
	
	if (key[4].double_flag == 1)
	{
		key[4].double_flag = 0;
		LCDPrintf(Line9, "K4 double  press   ");
	}
}


void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if (htim->Instance == TIM6)
	{
		data.tick10ms ++;
		Key_Serv_double();
	}
}
